package com.dv.driverless.service.impl;

import com.dv.common.exception.ServiceException;
import com.dv.common.utils.DateUtils;
import com.dv.driverless.domain.WarehouseArea;
import com.dv.driverless.domain.WarehouseLocation;
import com.dv.driverless.domain.vo.WarehouseLocationInitVo;
import com.dv.driverless.domain.vo.WarehouseLocationSteelCoilVo;
import com.dv.driverless.mapper.WarehouseAreaMapper;
import com.dv.driverless.mapper.WarehouseLocationMapper;
import com.dv.driverless.service.IWarehouseLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 库位信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-10
 */
@Service
public class WarehouseLocationServiceImpl implements IWarehouseLocationService
{
    @Autowired
    private WarehouseLocationMapper warehouseLocationMapper;

    @Autowired
    private WarehouseAreaMapper warehouseAreaMapper;

    /**
     * 查询库位信息
     *
     * @param id 库位信息主键
     * @return 库位信息
     */
    @Override
    public WarehouseLocation selectWarehouseLocationById(Long id)
    {
        return warehouseLocationMapper.selectWarehouseLocationById(id);
    }

    /**
     * 查询库位信息列表
     *
     * @param warehouseLocation 库位信息
     * @return 库位信息
     */
    @Override
    public List<WarehouseLocationSteelCoilVo> selectWarehouseLocationList(WarehouseLocation warehouseLocation)
    {
        return warehouseLocationMapper.selectWarehouseLocationList(warehouseLocation);
    }

    /**
     * 新增库位信息
     *
     * @param warehouseLocation 库位信息
     * @return 结果
     */
    @Override
    public int insertWarehouseLocation(WarehouseLocation warehouseLocation)
    {
        warehouseLocation.setCreateTime(DateUtils.getNowDate());
        return warehouseLocationMapper.insertWarehouseLocation(warehouseLocation);
    }

    /**
     * 修改库位信息
     *
     * @param warehouseLocation 库位信息
     * @return 结果
     */
    @Override
    public int updateWarehouseLocation(WarehouseLocation warehouseLocation)
    {
        warehouseLocation.setUpdateTime(DateUtils.getNowDate());
        return warehouseLocationMapper.updateWarehouseLocation(warehouseLocation);
    }

    /**
     * 批量删除库位信息
     *
     * @param ids 需要删除的库位信息主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseLocationByIds(Long[] ids)
    {
        return warehouseLocationMapper.deleteWarehouseLocationByIds(ids);
    }

    /**
     * 删除库位信息信息
     *
     * @param id 库位信息主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseLocationById(Long id)
    {
        return warehouseLocationMapper.deleteWarehouseLocationById(id);
    }

    /**
     * 初始化库位信息
     *
     * @param warehouseLocationInitVo
     * @return
     */
    @Override
    public int initLocation(WarehouseLocationInitVo warehouseLocationInitVo) {
        Long areaId = warehouseLocationInitVo.getAreaId();
        if (warehouseLocationMapper.selectWarehouseLocationByAreaId(areaId)>0){
            throw new ServiceException("库区已完成初始化，请勿重复！");
        }
        //钢卷最多堆两层
        Integer floor = 2;
        List<WarehouseLocation> warehouseLocations = new ArrayList<>();
        WarehouseArea warehouseArea = warehouseAreaMapper.selectWarehouseAreaById(areaId);
        String areaCode = warehouseArea.getAreaCode();
        Integer locatedRow = warehouseLocationInitVo.getLocatedRow();
        Integer locatedColumn = warehouseLocationInitVo.getLocatedColumn();
        for (Integer row = 1; row <= locatedRow; row++) {
            for (Integer column = 1; column <= locatedColumn; column++) {
                for (Integer level = 1; level <= floor; level++) {
                    if (column.equals(locatedColumn) && level.equals(floor)){
                        break;
                    }
                    WarehouseLocation warehouseLocation = new WarehouseLocation();
                    String code =
                            areaCode + String.format("%02d",row) + String.format("%02d",column) + String.format("%02d",level);
                    warehouseLocation.setCode(code);
                    warehouseLocation.setCreateTime(DateUtils.getNowDate());
                    warehouseLocation.setAreaId(areaId);
                    warehouseLocation.setLocatedRow(row);
                    warehouseLocation.setLocatedColumn(column);
                    warehouseLocation.setLocatedFloor(level);
                    warehouseLocations.add(warehouseLocation);
                }
            }
        }
        return warehouseLocationMapper.batchInsertWarehouseLocation(warehouseLocations);
    }

    /**
     * 批量锁定库位
     *
     * @param ids
     * @return
     */
    @Override
    public int lockLocation(List<Long> ids) {
        if (ids.isEmpty()){
            return 0;
        }
        return this.batchChangeWarehouseLocationStatus(ids,2);
    }

    /**
     * 批量修改库位状态
     *
     * @param ids
     * @param status
     * @return
     */
    public int batchChangeWarehouseLocationStatus(List<Long> ids, Integer status){
       return warehouseLocationMapper.batchChangeWarehouseLocationStatus(ids,status);
    }
}
